package org.workplicity.android.cia.ui.elogfragments;

import org.workplicity.android.cia.R;
import org.workplicity.android.cia.ui.ELogActivity;
import org.workplicity.android.cia.ui.ElogEditActivity;
import org.workplicity.android.cia.ui.mainfragments.ContentFragment;
import org.workplicity.android.cia.util.CIAHelper;
import org.workplicity.entry.Comment;
import org.workplicity.util.Helper;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.Fragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class CommentEditMainFragment extends Fragment {
	
	/**
	 * View
	 */
	View v;
	
	/**
     * Controls
     */
    Button addButton;
    
 	/**
 	 * Parent Activity
 	 */
	ElogEditActivity myActivity;
    
    String enteredComment;
    
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		if(v == null) {
			v = inflater.inflate(R.layout.comment_edit_main_fragment, container, false);
		}
		
		// initialize the local ref to the parent activity
        myActivity = (ElogEditActivity)getActivity();
		
		LayoutInflater factory = LayoutInflater.from(getActivity());
		final AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
		final View textEntryView;
		alert.setTitle(R.string.create_comment); 
		alert.setMessage(R.string.commentdetail); 
		
		textEntryView = factory.inflate(R.layout.createcomment, null);
		
		// associate the correct view to the alert
		alert.setView(textEntryView);
		
		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				
				// set the dirty flag
            	myActivity.getApp().setCurrentElogDirty(true);

				EditText mUserText;
				
				mUserText = (EditText) textEntryView.findViewById(R.id.create_comment_text);
				enteredComment = mUserText.getText().toString();

				if(enteredComment.length() == 0)
		            return;

		        Comment comment = new Comment();

		        comment.setText(enteredComment);
		        comment.setUserId(myActivity.getApp().getLoggedInUser().getId());

		        // add the comment to the model
		        myActivity.getApp().getCurrentElog().getComments().add(0,comment);
		        
		        // add the audit entry
		        myActivity.getApp().updateAudit("Added comment.");

		        // refresh the activty
		        CommentEditFragment frag = (CommentEditFragment) getFragmentManager().findFragmentById(R.id.comment_edit_fragment);
		        frag.populateTitles();
		        
				dialog.cancel();
				
			}
		});

		// cancel back to the previous screen
		alert.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						dialog.cancel();
					}
				});
		
		
		
		this.addButton = (Button)v.findViewById(R.id.logEditCommentsAddComment);
		this.addButton.setOnClickListener(new View.OnClickListener() {
		    @Override
		    public void onClick(View v) {
		    	// open a dialog to get the new comment
		    	// show the password alert if necessary 
				alert.show();
		    }
		});
		
		return v;
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
    
    public void onItemSelected(AdapterView parent, View v, int position, long id) { 
		// this will do something
    	
	}

}
